/*set working directory*/
/*ssc install blindschemes*/
/*findit renvars*/
/*ssc install relogit*/

/* Roustness Checks for Figure 5.4*/ 

/*Data Set-Up*/
clear
use "Data/American INGOs Geo Data with Attributes 2020.dta"
encode name, gen(orgid)
sort orgid

gen scode2=1
gen scode20=1
gen scode31=1
gen scode40=1
gen scode41=1 
gen scode42=1 
gen scode51=1 
gen scode52=1 
gen scode53=1 
gen scode54=1 
gen scode55=1 
gen scode56=1 
gen scode57=1
gen scode58=1  
gen scode60=1 
gen scode70=1 
gen scode80=1 
gen scode90=1 
gen scode91=1 
gen scode92=1 
gen scode93=1 
gen scode94=1 
gen scode95=1 
gen scode100=1 
gen scode101=1 
gen scode110=1 
gen scode115=1 
gen scode130=1 
gen scode135=1 
gen scode140=1 
gen scode145=1 
gen scode150=1 
gen scode155=1 
gen scode160=1 
gen scode165=1 
gen scode200=1 
gen scode205=1
gen scode210=1
gen scode211=1
gen scode212=1
gen scode220=1
gen scode221=1
gen scode223=1
gen scode225=1
gen scode230=1
gen scode232=1
gen scode235=1
gen scode255=1
gen scode260=1
gen scode265=1
gen scode290=1
gen scode305=1
gen scode310=1
gen scode315=1
gen scode316=1
gen scode317=1
gen scode325=1
gen scode331=1
gen scode338=1
gen scode339=1
gen scode341=1
gen scode345=1
gen scode348=1
gen scode343=1
gen scode344=1
gen scode346=1
gen scode347=1
gen scode349=1
gen scode350=1
gen scode352=1
gen scode355=1
gen scode359=1
gen scode360=1
gen scode365=1
gen scode366=1
gen scode367=1
gen scode368=1
gen scode369=1
gen scode370=1
gen scode371=1
gen scode372=1
gen scode373=1
gen scode375=1
gen scode380=1
gen scode385=1
gen scode390=1
gen scode395=1
gen scode402=1
gen scode403=1
gen scode404=1
gen scode411=1
gen scode420=1
gen scode432=1
gen scode433=1
gen scode434=1
gen scode435=1
gen scode436=1
gen scode437=1
gen scode438=1
gen scode439=1
gen scode450=1
gen scode451=1
gen scode452=1
gen scode461=1
gen scode471=1
gen scode475=1
gen scode481=1
gen scode482=1
gen scode483=1
gen scode484=1
gen scode490=1
gen scode500=1
gen scode501=1
gen scode510=1
gen scode511=1
gen scode516=1
gen scode517=1
gen scode520=1
gen scode522=1
gen scode530=1
gen scode531=1
gen scode540=1
gen scode541=1
gen scode551=1
gen scode552=1
gen scode553=1
gen scode560=1
gen scode565=1
gen scode570=1
gen scode571=1
gen scode572=1
gen scode580=1
gen scode581=1
gen scode590=1
gen scode591=1
gen scode600=1
gen scode615=1
gen scode616=1
gen scode620=1
gen scode625=1
gen scode630=1
gen scode640=1
gen scode645=1
gen scode651=1
gen scode652=1
gen scode660=1
gen scode663=1
gen scode666=1
gen scode667=1
gen scode670=1
gen scode678=1
gen scode679=1
gen scode680=1
gen scode690=1
gen scode692=1
gen scode694=1
gen scode696=1
gen scode698=1
gen scode700=1
gen scode701=1
gen scode702=1
gen scode703=1
gen scode704=1
gen scode705=1
gen scode710=1
gen scode712=1
gen scode713=1
gen scode730=1
gen scode731=1
gen scode732=1
gen scode740=1
gen scode750=1
gen scode760=1
gen scode770=1
gen scode771=1
gen scode775=1
gen scode780=1
gen scode781=1
gen scode790=1
gen scode800=1
gen scode811=1
gen scode812=1
gen scode816=1
gen scode820=1
gen scode830=1
gen scode835=1
gen scode840=1
gen scode850=1
gen scode860=1
gen scode900=1
gen scode910=1
gen scode920=1
gen scode935=1
gen scode940=1
gen scode946=1
gen scode947=1
gen scode950=1
gen scode955=1
gen scode970=1
gen scode983=1
gen scode986=1
gen scode987=1
gen scode990=1

reshape long scode, i(orgid) j(countrycode)
gen membership=.
foreach x of varlist country1-country172 {
      replace membership=1 if `x'==countrycode
        }
replace membership=0 if membership==.
drop country1-country172
drop scode
rename countrycode scode
replace scode=255 if scode==260
replace scode=255 if scode==265
replace scode=316 if scode==315
replace scode=490 if scode==484
replace scode=666 if scode==667
replace scode=679 if scode==678
replace scode=679 if scode==680
replace scode=710 if scode==713
replace scode=732 if scode==730
replace scode=345 if scode==348
replace scode=510 if scode==511

encode state, generate(DC2)
recode DC2 (min/9=0) (10=1) (11=1) (12/31=0) (32=1) (32/59=0) (60=1) (61/max=0)
label drop DC2
label variable DC2 "DC Headquarters"

tempfile USA_INGOs_by_Country_2020
save `USA_INGOs_by_Country_2020'

/*generate density variables from the TSMOD*/
use "Data/TSMOD STATA 2.dta", clear
fsum, label 
keep if year==2013
encode orgname, gen(orgid)

foreach x of var algeria angola	benin	botswan	burkfas	burundi	cameroon	capeverd	centafre	chad	comoros	congo_br	congo_dr	djibouti	egypt	guinea_equat	eritrea	ethiopia	gabon	gambia	ghana	guinea	guin_bis	ivcoast	kenya	lesotho	liberia	libya	madagasc	malawi	mali	mauritan	mauritiu	morocco	mozamb	namibia	niger	nigeria	rwanda	saotomeprincipe	senegal	seychel	sier_le	somalia	sth_afr	sudan	swazilan	tanzania	togo	tunisia	uganda	westernsahara	zambia	zimbabwe	argent	antigua	bahamas	barbados	belize	bermuda	bolivia	brazil	canada	chile	colombia	cost_ric	cuba	dominica	dom_rep	ecuador	el_salv	grenada	guatemal	guyana	haiti	honduras	jamaica	martinique	mexico	neth_ant	nicaragu	panama	paraguay	peru	puerto_r	st_kitts	st_lucia	st_martin	st_vince	suriname	trinidad	usa	uruguay	venezuel	virgin	afghanis	bahrain	banglade	bhutan	brun_dar	myanmar	cambodia	china_pr	east_tim	hong_kon	india	indonesi	iran	iraq	israel	japan	jordan	kor_rep	kazakhst	kuwait	kyrgyzstan	laos	lebanon	macao	malaysia	maldives	mongolia	n_korea	nepal	oman	pakistan	palestin	philippi	qatar	saudi_ar	singapore	sri_lank	syria	taiwan	tajiksta	thailand	turkmenistan	u_ar_em	uzbekist	vietnam	yemen	australi	fiji	kiribati	marshall_is	micronesia	new_caled	new_zeal	palau	papua_ng	polynesia	samoa	solomon_is	tahiti	tonga	tuvalu	vanuatu	albania	andorra	armenia	austria	azerbaij	belarus	belgium	bosnia	bulgaria	chechnya	croatia	cyprus	czech_re	denmark	estonia	faeroeis	finland	france	guiana_french	georgia	germany	gibraltor	greece	greenland	guadeloupe	hungary	iceland	ireland	isle_of_man	italy	latvia	liechtenstein	lithuani	luxembou	macedonia	malta	moldova	monaco	montenegro	netherla	norway	poland	portugal	reunion	romania	russia	san_marino	serbia	slovakia	slovenia	spain	sweden	switzerl	turkey	uk	ukraine	vatican	yugoslav { 	
 rename `x' mem`x' 
} 

egen orgyear = concat(orgid year)
sort orgid year
quietly by orgid year:  gen dup = cond(_N==1,0,_n)
tabulate dup
drop if dup>1

drop seriesid yearbookid hqtxt members_1 members_2 members_3 members_4 members_5 finance_self finance_founds finance_corps finance_govigo finance_othr ident_1 ident_2 goal_txt cons1 cons2 cons3 cons4 cons5 cons_txt hq2 igo1 igo2 igo3 igo4 igo5 igo6 igo_txt othercountry entryyear yearbookcode fillyear count foundyrflag norso after89 oecd1 dup 

reshape long mem, i(orgyear) j(country, string)
gen sname=country
gen scode=.
run "Scode Transformation.do"
drop if scode==.
replace scode=255 if scode==260
replace scode=255 if scode==265
replace scode=316 if scode==315
replace scode=679 if scode==678
replace scode=679 if scode==680
replace scode=732 if scode==730
replace scode=510 if scode==511
tempfile Org_Country_2013
save `Org_Country_2013'

gen usahq=. 
replace usahq=1 if hq1==12
replace usahq=1 if hq1==14
replace usahq=1 if hq1==15
replace usahq=1 if hq1==16
replace usahq=0 if usahq==.
keep if usahq==1
tempfile USA_Org_Country_2013
save `USA_Org_Country_2013'

use `Org_Country_2013', clear
collapse (sum) mem (max) scode, by (country)
rename mem total_ingos
sort scode
tempfile INGOs_Country_Sector_2013
save `INGOs_Country_Sector_2013'

use `USA_Org_Country_2013', clear
collapse (sum) mem (max) scode, by (country)
rename mem usa_total_ingos
sort scode
tempfile USA_INGOs_Country_Sector_2013
save `USA_INGOs_Country_Sector_2013'

use `USA_INGOs_by_Country_2020', clear
sort scode
merge m:1 scode using `INGOs_Country_Sector_2013'
replace total_ingos=0 if _merge==1
drop if _merge==2
drop _merge
sort scode
merge m:1 scode using `USA_INGOs_Country_Sector_2013'
replace usa_total_ingos=0 if _merge==1
drop if _merge==2
drop _merge
tempfile formerging
save `formerging'

use "Data/V-Dem-CY-Full+Others-v12.dta", clear
keep if year==2019
keep country_name year COWcode v2csreprss e_wbgi_cce e_wbgi_pve e_wb_pop e_gdppc v2x_polyarchy
rename COWcode scode
sort scode
drop if scode==.
merge 1:m scode using `formerging'
drop _merge
tempfile formerging
save `formerging', replace

clear
import excel "Data/us_foreignaid_greenbook.xlsx", sheet("Economic and Military Aid") firstrow
keep if AssistanceCategory=="Economic"
rename FiscalYear year
keep if year==2019
collapse (sum) ObligationsConstantDollars, by(Country)
rename Country sname
generate scode=.
run "Scode Transformation.do"
drop if scode==.
rename sname country
rename ObligationsConstantDollars us_aid
sort scode 
merge 1:m scode using `formerging'
replace us_aid=0 if _merge==2
drop if _merge==1
drop _merge
tempfile formerging
save `formerging', replace

use "Data/atop5_1dy.dta", clear
keep if mem1==2
keep if year==2018
replace year=year+1
keep mem2 year atopally
rename mem2 scode
sort scode
merge 1:m scode using `formerging'
replace atopally=0 if _merge==2
drop if _merge==1
drop _merge
tempfile formerging
save `formerging', replace

use "Data/US Imports 2019.dta", clear
drop if scode==.
sort scode 
merge 1:m scode using `formerging'
replace ImportUSThousand=0 if _merge==2
drop if _merge==1
drop _merge
sort scode
tempfile formerging
save `formerging', replace

clear
import excel "Data/Data Set Tracking Media Attention in the US to Countries (NY Times).xlsx", firstrow 
keep Scode Country year2019
rename Scode scode
rename Country country
rename year2019 media
duplicates tag scode, gen(isdup) 
drop if isdup>0
sort scode
merge 1:m scode using `formerging'

renvars usa_total_ingos total_ingos, postfix(_2013)

gen total_ingos_2013_sq=total_ingos_2013^2

gen usa_total_ingos_2013_sq=usa_total_ingos_2013^2

/*Analysis*/

drop if scode==2
gen lnaid=ln(us_aid+1) 
gen lnimports=ln(ImportUSThousand+1)
gen lnrevenue=ln(revenue+1)
gen lnpop=ln(e_wb_pop+1)
gen lngdp=log(e_gdppc+1)
gen lnmedia=ln(media+1)

egen stdusingos=std(usa_total_ingos_2013)
egen stdusingos_sq=std(usa_total_ingos_2013_sq)

label variable lnrevenue "Revenue"
label variable geoscopenumeric "Geographic Scope"
label variable faithbased "Faith-based"
label variable lnaid "U.S. Aid"
label variable lnmedia "Media Attention"
label variable lngdp "GDP per capita"
label variable lnpop "Population"
label variable e_wbgi_cce "Control of Corruption"
label variable v2csreprss "CSO Repression"
label variable atopally "U.S. Ally"
label variable stdusingos "Density"
label variable stdusingos_sq "Density Squared"

/*Table with Regression Output*/
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss atopally, cluster(orgid)
outreg2 using "Tables/figure5-4.tex", ctitle(Baseline Model) bdec(2) tdec(2) rdec(2) alpha(.01, .05, .1) replace
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss atopally stdusingos stdusingos_sq, cluster(orgid)
outreg2 using "Tables/figure5-4.tex", ctitle(Full Model) bdec(2) tdec(2) rdec(2) alpha(.01, .05, .1) append

/*Rare Events Logit*/
relogit membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss atopally, cluster(orgid)
estimates store Baseline
relogit membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss atopally stdusingos stdusingos_sq, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(lnrevenue = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-4_Relogit.pdf", as(pdf) replace

/*Organization indicator variables*/
reg membership lnaid lnmedia lngdp lnpop v2csreprss atopally i.orgid, cluster(orgid)
estimates store Baseline
reg membership lnaid lnmedia lngdp lnpop v2csreprss atopally i.orgid stdusingos stdusingos_sq, cluster(orgid)
estimates store Full
coefplot Baseline Full, keep(*lnrevenue *geoscopenumeric *faithbased *DC2 *lnaid ///
	*lnmedia *lngdp *e_wbgi_cce *v2csreprss *atopally *stdusingos *stdusingos_sq) ///
	xline(0) headings(lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(med) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-4_Org_indicators.pdf", as(pdf) replace

/*Control for Corruption*/
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop e_wbgi_cce v2csreprss atopally, cluster(orgid)
estimates store Baseline
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop e_wbgi_cce v2csreprss atopally stdusingos stdusingos_sq, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(lnrevenue = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-4_Corruption.pdf", as(pdf) replace

/*Control for political stability*/
label variable e_wbgi_pve "Political Stability"
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop e_wbgi_pve v2csreprss atopally, cluster(orgid)
estimates store Baseline
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop e_wbgi_pve v2csreprss atopally stdusingos stdusingos_sq, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(lnrevenue = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-4_Stability.pdf", as(pdf) replace

/*Control for democracy*/
label variable v2x_polyarchy "Electoral Democracy"
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2x_polyarchy v2csreprss atopally, cluster(orgid)
estimates store Baseline
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2x_polyarchy v2csreprss atopally stdusingos stdusingos_sq, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(lnrevenue = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-4_Democracy.pdf", as(pdf) replace
	
/*Control for US imports instead of alliances*/
label variable lnimports "U.S. Imports"
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss lnimports, cluster(orgid)
estimates store Baseline
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss lnimports stdusingos stdusingos_sq, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(lnrevenue = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-4_Imports.pdf", as(pdf) replace

*Global South only
summarize e_gdppc, detail
gen develcountry=.
replace develcountry=1 if e_gdppc<17.73328
replace develcountry=0 if e_gdppc>=17.73328
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss atopally if develcountry==1, cluster(orgid)
estimates store Baseline
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss atopally stdusingos stdusingos_sq if develcountry==1, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(lnrevenue = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-4_GlobalSouth.pdf", as(pdf) replace

/*Using all INGOs for density*/
egen stdusingos_all=std(total_ingos_2013)
egen stdusingos_sq_all=std(total_ingos_2013_sq)
label variable stdusingos_all "Density"
label variable stdusingos_sq_all "Density Squared"
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss atopally, cluster(orgid)
estimates store Baseline
reg membership lnrevenue geoscopenumeric faithbased DC2 lnaid lnmedia lngdp lnpop v2csreprss atopally stdusingos_all stdusingos_sq_all, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(lnrevenue = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-4_AllINGOs.pdf", as(pdf) replace

/*Include Sector Indicator Variables*/
replace revisedsector="Democracy" if revisedsector=="UNKNOWN, democracy?"
encode revisedsector, gen(pop)
reg membership lnrevenue geoscopenumeric faithbased DC2 i.pop lnaid lnmedia lngdp lnpop v2csreprss atopally, cluster(orgid)
estimates store Baseline
reg membership lnrevenue geoscopenumeric faithbased DC2 i.pop lnaid lnmedia lngdp lnpop v2csreprss atopally  stdusingos stdusingos_sq, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(lnrevenue = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-4_SectorIndicators.pdf", as(pdf) replace


	
	
/*Robustness Checks for Figure 5.5*/

use "Data/TSMOD STATA 2.dta", clear
fsum, label 

encode seriesid, gen(orgid)
foreach x of var algeria angola	benin	botswan	burkfas	burundi	cameroon	capeverd	centafre	chad	comoros	congo_br	congo_dr	djibouti	egypt	guinea_equat	eritrea	ethiopia	gabon	gambia	ghana	guinea	guin_bis	ivcoast	kenya	lesotho	liberia	libya	madagasc	malawi	mali	mauritan	mauritiu	morocco	mozamb	namibia	niger	nigeria	rwanda	saotomeprincipe	senegal	seychel	sier_le	somalia	sth_afr	sudan	swazilan	tanzania	togo	tunisia	uganda	westernsahara	zambia	zimbabwe	argent	antigua	bahamas	barbados	belize	bermuda	bolivia	brazil	canada	chile	colombia	cost_ric	cuba	dominica	dom_rep	ecuador	el_salv	grenada	guatemal	guyana	haiti	honduras	jamaica	martinique	mexico	neth_ant	nicaragu	panama	paraguay	peru	puerto_r	st_kitts	st_lucia	st_martin	st_vince	suriname	trinidad	usa	uruguay	venezuel	virgin	afghanis	bahrain	banglade	bhutan	brun_dar	myanmar	cambodia	china_pr	east_tim	hong_kon	india	indonesi	iran	iraq	israel	japan	jordan	kor_rep	kazakhst	kuwait	kyrgyzstan	laos	lebanon	macao	malaysia	maldives	mongolia	n_korea	nepal	oman	pakistan	palestin	philippi	qatar	saudi_ar	singapore	sri_lank	syria	taiwan	tajiksta	thailand	turkmenistan	u_ar_em	uzbekist	vietnam	yemen	australi	fiji	kiribati	marshall_is	micronesia	new_caled	new_zeal	palau	papua_ng	polynesia	samoa	solomon_is	tahiti	tonga	tuvalu	vanuatu	albania	andorra	armenia	austria	azerbaij	belarus	belgium	bosnia	bulgaria	chechnya	croatia	cyprus	czech_re	denmark	estonia	faeroeis	finland	france	guiana_french	georgia	germany	gibraltor	greece	greenland	guadeloupe	hungary	iceland	ireland	isle_of_man	italy	latvia	liechtenstein	lithuani	luxembou	macedonia	malta	moldova	monaco	montenegro	netherla	norway	poland	portugal	reunion	romania	russia	san_marino	serbia	slovakia	slovenia	spain	sweden	switzerl	turkey	uk	ukraine	vatican	yugoslav { 	
 rename `x' mem`x' 
} 

egen orgyear = concat(orgid year)

sort orgid year
quietly by orgid year:  gen dup = cond(_N==1,0,_n)
tabulate dup
drop if dup>1

drop seriesid yearbookid hqtxt members_1 members_2 members_3 members_4 members_5 finance_self finance_founds finance_corps finance_govigo finance_othr ident_1 ident_2 goal_txt cons1 cons2 cons3 cons4 cons5 cons_txt hq2 igo1 igo2 igo3 igo4 igo5 igo6 igo_txt othercountry entryyear yearbookcode fillyear count foundyrflag norso after89 oecd1 dup 

reshape long mem, i(orgyear) j(country, string)
gen sname=country
gen scode=.
run "Scode Transformation.do"
drop if scode==.
replace scode=255 if scode==260
replace scode=255 if scode==265
replace scode=316 if scode==315
replace scode=679 if scode==678
replace scode=679 if scode==680
replace scode=732 if scode==730
replace scode=510 if scode==511
sort orgid country year
by orgid country: gen mem_lag2=mem[_n-1]
by orgid country: gen mem_lag4=mem[_n-2]
tempfile Org_Country_Year
save `Org_Country_Year'

gen usahq=. 
replace usahq=1 if hq1==12
replace usahq=1 if hq1==14
replace usahq=1 if hq1==15
replace usahq=1 if hq1==16
replace usahq=0 if usahq==.
keep if usahq==1
tempfile USA_Org_Country_Year
save `USA_Org_Country_Year'

use `Org_Country_Year', clear
collapse (sum) mem (max) scode, by (country year)
rename mem total_ingos
sort year scode
tempfile INGOs_Country_Year_Sector
save `INGOs_Country_Year_Sector'

use `USA_Org_Country_Year', clear
collapse (sum) mem (max) scode, by (country year)
rename mem usa_total_ingos
sort year scode
tempfile USA_INGOs_Country_Year_Sector
save `USA_INGOs_Country_Year_Sector'

use `USA_Org_Country_Year', clear
sort year scode
merge m:1 year scode using `INGOs_Country_Year_Sector'
drop _merge
sort year scode
merge m:1 year scode using `USA_INGOs_Country_Year_Sector'
drop _merge
sort scode year
tempfile formerging
save `formerging'

clear
import excel "Data/us_foreignaid_greenbook.xlsx", sheet("Economic and Military Aid") firstrow
keep if AssistanceCategory=="Economic"
rename FiscalYear year
keep if year==1992 | year==1994 | year==1996 | year==1998 | year==2000 | year==2002 | year==2004 | year==2006 | year==2008 | year==2010 | year==2012
replace year=year+1
collapse (sum) ObligationsConstantDollars, by(Country year)
rename Country sname
generate scode=.
run "Scode Transformation.do"
drop if scode==.
rename sname country
drop if country=="Serbia and Montenegro, Former" & year==2007 /*to remove a duplicate observation*/
rename ObligationsConstantDollars us_aid
sort scode year
merge 1:m scode year using `formerging'
replace us_aid=0 if _merge==2
drop if _merge==1
drop _merge
tempfile formerging
save `formerging'

use "Data/V-Dem-CY-Full+Others-v12.dta", clear
keep if year==1992 | year==1994 | year==1996 | year==1998 | year==2000 | year==2002 | year==2004 | year==2006 | year==2008 | year==2010 | year==2012
replace year=year+1
keep country_name year COWcode v2csreprss e_wbgi_cce e_wb_pop e_gdppc e_wbgi_pve v2x_polyarchy
rename COWcode scode
drop if scode==.
sort scode year
merge 1:m scode year using `formerging'
drop _merge
tempfile formerging
save `formerging'

clear
import excel "Data/Data Set Tracking Media Attention in the US to Countries (NY Times).xlsx", firstrow 
rename Scode scode
rename Country country
duplicates tag scode, gen(isdup) 
drop if isdup>0
drop P Q R S T U V W X Y Z isdup
reshape long year, i(scode) j(variable)
rename year media
rename variable year
drop if year==1991 | year==2019
sort scode year
merge 1:m scode year using `formerging'
drop _merge
tempfile formerging
save `formerging'

use "Data/atop5_1dy.dta", clear
keep if mem1==2
keep if year==1992 | year==1994 | year==1996 | year==1998 | year==2000 | year==2002 | year==2004 | year==2006 | year==2008 | year==2010 | year==2012
replace year=year+1
keep mem2 year atopally
rename mem2 scode
sort scode year
merge 1:m scode year using `formerging'
replace atopally=0 if _merge==2
drop if _merge==1
drop _merge
tempfile formerging
save `formerging'

clear
import delimited "Data/Dyadic_COW_4.0.csv"
keep if importer1=="United States of America"
keep if year==1992 | year==1994 | year==1996 | year==1998 | year==2000 | year==2002 | year==2004 | year==2006 | year==2008 | year==2010 | year==2012
replace year=year+1
rename ccode2 scode
sort scode year
merge 1:m scode year using `formerging'
replace flow1=0 if _merge==2
drop if _merge==1
drop _merge
tempfile formerging
save `formerging'

use "Data/NGOrepression.dta", clear 
sort ccode year
quietly by ccode year:  gen dup = cond(_N==1,0,_n)
drop if dup==2
drop dup
keep if year==1992 | year==1994 | year==1996 | year==1998 | year==2000 | year==2002 | year==2004 | year==2006 | year==2008 | year==2010 | year==2012
replace year=year+1
rename ccode scode
sort scode year
merge 1:m scode year using `formerging'
drop if _merge==1
drop _merge

gen total_ingos_sq=total_ingos^2
gen usa_total_ingos_sq=usa_total_ingos^2
tempfile TSMO_Data_for_Lagged_Merges
save `TSMO_Data_for_Lagged_Merges'

collapse total_ingos total_ingos_sq  usa_total_ingos usa_total_ingos_sq, by(scode year)
tsset scode year

gen total_ingos_L2=L2.total_ingos
gen total_ingos_L4=L4.total_ingos
gen total_ingos_L6=L6.total_ingos
gen total_ingos_sq_L2=L2.total_ingos_sq
gen total_ingos_sq_L4=L4.total_ingos_sq
gen total_ingos_sq_L6=L6.total_ingos_sq
gen usa_total_ingos_L2=L2.usa_total_ingos
gen usa_total_ingos_L4=L4.usa_total_ingos
gen usa_total_ingos_L6=L6.usa_total_ingos
gen usa_total_ingos_sq_L2=L2.usa_total_ingos_sq
gen usa_total_ingos_sq_L4=L4.usa_total_ingos_sq
gen usa_total_ingos_sq_L6=L6.usa_total_ingos_sq
tempfile country_level_ingo_merges
save `country_level_ingo_merges'

use `TSMO_Data_for_Lagged_Merges', clear

sort scode year
merge m:1 scode year using `country_level_ingo_merges'
drop _merge

gen lnaid=ln(us_aid+1) 
gen lnpop=ln(e_wb_pop+1)
gen lngdp=log(e_gdppc+1)
gen lnmedia=ln(media+1)

* set up 
drop if scode==2

egen stdusingos_L2=std(usa_total_ingos_L2)
egen stdusingos_sq_L2=std(usa_total_ingos_sq_L2)

egen stdusingos_L4=std(usa_total_ingos_L4)
egen stdusingos_sq_L4=std(usa_total_ingos_sq_L4)

egen stdusingos_L6=std(usa_total_ingos_L6)
egen stdusingos_sq_L6=std(usa_total_ingos_sq_L6)

egen stdmem_lag2=std(mem_lag2)
egen stdmem_lag4=std(mem_lag4)

label variable lnaid "U.S. Aid"
label variable lnmedia "Media Attention"
label variable lngdp "GDP per capita"
label variable lnpop "Population"
label variable v2csreprss "CSO Repression"
label variable atopally "U.S. Ally"
label variable stdusingos_L4 "Density"
label variable stdusingos_sq_L4 "Density Squared"
label variable stdmem_lag2 "Lagged DV"
label variable stdusingos_L2 "Density"
label variable stdusingos_sq_L2 "Density Squared"
label variable stdusingos_L6 "Density"
label variable stdusingos_sq_L6 "Density Squared"
label variable year "Year"
label variable age "Age"

egen panelvar = group(scode orgid)
sort panelvar year
quietly by panelvar year:  gen dup = cond(_N==1,0,_n)
drop if dup>0
xtset panelvar year
keep if year>1992

/*Table with Regression Output*/
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year, cluster(orgid)
outreg2 using "Tables/figure5-5.tex", ctitle(Baseline Model) bdec(2) tdec(2) rdec(2) alpha(.01, .05, .1) replace
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
outreg2 using "Tables/figure5-5.tex", ctitle(Full Model) bdec(2) tdec(2) rdec(2) alpha(.01, .05, .1) append

/*Rare Events Logit*/
relogit mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year, cluster(orgid)
estimates store Baseline
relogit mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_Relogit.pdf", as(pdf) replace

/*Rare Events Logit with Lagged DV*/
relogit mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdmem_lag2, cluster(orgid)
estimates store Baseline
relogit mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdmem_lag2 stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_Relogit_LaggedDV.pdf", as(pdf) replace

/*Linear Probability Model with Lagged DV*/
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdmem_lag2, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdmem_lag2 stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_laggedDV.pdf", as(pdf) replace

/*Random Effects*/
xtreg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year, re cluster(panelvar)
estimates store Baseline
xtreg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdusingos_L2 stdusingos_sq_L2, re cluster(panelvar)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_RE.pdf", as(pdf) replace

/*Random Effects and Lagged DV*/
xtreg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdmem_lag2, re cluster(panelvar)
estimates store Baseline
xtreg mem age  lnaid lnmedia lngdp lnpop v2csreprss atopally year stdmem_lag2 stdusingos_L2 stdusingos_sq_L2, re cluster(panelvar)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_RE_laggedDV.pdf", as(pdf) replace

/*LPM with 4-year density lag*/
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdusingos_L4 stdusingos_sq_L4, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_4yearlag.pdf", as(pdf) replace

/*LPM with 6-year density lag*/
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdusingos_L6 stdusingos_sq_L6, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_6yearlag.pdf", as(pdf) replace

/*Control for control of corruption*/
label variable e_wbgi_cce "Control of Corruption"
reg mem age lnaid lnmedia lngdp lnpop v2csreprss e_wbgi_cce atopally year, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop v2csreprss e_wbgi_cce atopally year stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_corruptioncontrol.pdf", as(pdf) replace

/*Control for political stability*/
label variable e_wbgi_pve "Political Stability"
reg mem age lnaid lnmedia lngdp lnpop v2csreprss e_wbgi_pve atopally year, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop v2csreprss e_wbgi_pve atopally year stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_politicalstability.pdf", as(pdf) replace

/*Control for democracy*/
label variable v2x_polyarchy "Electoral Democracy"
reg mem age lnaid lnmedia lngdp lnpop v2csreprss v2x_polyarchy atopally year, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop v2csreprss v2x_polyarchy atopally year stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_democracy.pdf", as(pdf) replace

/*Control for U.S. imports*/
gen lnimports=log(flow1+1)
label variable lnimports "U.S. Imports"
reg mem age lnaid lnmedia lngdp lnpop v2csreprss lnimports year, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop v2csreprss lnimports year stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_imports.pdf", as(pdf) replace

/*Chaudhry measure of foreign funding restrictions*/
label variable ff "NGO Restrictions"
reg mem age lnaid lnmedia lngdp lnpop ff atopally year, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop ff atopally year stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_foreignfunding.pdf", as(pdf) replace

/*Only Global South countries*/
summarize e_gdppc, detail
gen develcountry=.
replace develcountry=1 if e_gdppc<17.73328
replace develcountry=0 if e_gdppc>=17.73328
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year if develcountry==1, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdusingos_L2 stdusingos_sq_L2 if develcountry==1, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_GlobalSouth.pdf", as(pdf) replace

/*Using all INGOs for density*/
egen stdusingos_all=std(total_ingos_L2)
egen stdusingos_sq_all=std(total_ingos_sq_L2)
label variable stdusingos_all "Density"
label variable stdusingos_sq_all "Density Squared"
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year, cluster(orgid)
estimates store Baseline
reg mem age lnaid lnmedia lngdp lnpop v2csreprss atopally year stdusingos_all stdusingos_sq_all, cluster(orgid)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_AllINGOs.pdf", as(pdf) replace

/*Org fixed effects*/
reg mem lnaid lnmedia lngdp lnpop v2csreprss atopally year i.orgid, cluster(orgid)
estimates store Baseline
reg mem lnaid lnmedia lngdp lnpop v2csreprss atopally year i.orgid stdusingos_L2 stdusingos_sq_L2, cluster(orgid)
estimates store Full
coefplot Baseline Full, keep(*age *lnaid *lnmedia *lngdp *lnpop ///
	*v2csreprss *atopally *year *v2csreprss *atopally ///
	*stdusingos_L2 *stdusingos_sq_L2) xline(0) ///
	headings(age = "{bf:INGO Attributes}" ///
	lnaid = "{bf:Country Attributes}", labsize(medlarge)) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.2fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure5-5_Org_indicators.pdf", as(pdf) replace
